/*
 * 表格界面项目
*/
#include "mymainwindow.h"
#include "ui_mymainwindow.h"

#include "formnew_01.h"
extern Formnew_01 *pmid;

myMainWindow::myMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::myMainWindow)
{
    ui->setupUi(this);

    //实例化【界面3】对象
    this->tedit = new textEdit;

    QTime starttime = QTime::currentTime();//程序进入开始时间

    //给【界面0】发送信号，返回【界面0】
    connect(ui->pushButton_2, &QPushButton::clicked, this, &myMainWindow::emit_signals);

    //给Formnew_01【界面2】发送信号，打印出点击按键的实时系统时间
    connect(this, &myMainWindow::emit_pushButton, pmid, &Formnew_01::formnew_01_Receive_the_response);//【添加、覆盖、修改】信号
    connect(this, &myMainWindow::emit_pushButton_04, pmid, &Formnew_01::formnew_01_Receive_the_response);//添加行
    connect(this, &myMainWindow::emit_pushButton_05, pmid, &Formnew_01::formnew_01_Receive_the_response);//删除行
    connect(this, &myMainWindow::emit_find_table_text, pmid, &Formnew_01::formnew_01_Receive_the_response);//查找表格文本
    connect(this, &myMainWindow::emit_delete_table_text, pmid, &Formnew_01::formnew_01_Receive_the_response);//删除表格文本
    connect(this, &myMainWindow::emit_show_hide_formnew_01_ui, pmid, &Formnew_01::formnew_01_Receive_the_response);//显示隐藏表格
    connect(this, &myMainWindow::page04, pmid, &Formnew_01::formnew_01_Receive_the_response);//隐藏【界面3】，打开【界面1】
    connect(this->tedit, &textEdit::page03, pmid, &Formnew_01::formnew_01_Receive_the_response);//隐藏【界面1】，打开【界面3】



    connect(ui->pushButton_10, &QPushButton::clicked, this, [=](){
        //隐藏【界面1】，打开【界面3】
        this->hide();
        this->tedit->show();
        emit this->tedit->page03(TRAM_to_textedit, NULL);
    });


    connect(this->tedit, &textEdit::back_page3, this, [=](){
        //接收【界面3】传递来的信号，隐藏【界面3】，打开【界面1】
        this->tedit->hide();
        this->show();
        emit this->page04(TRAM_back_tablewiaget,NULL);
    });



    QString COM1("0");
    QString COM2("1");
    QString COM3("2");
    ui->comboBox->addItem(COM1);
    ui->comboBox->addItem(COM2);
    ui->comboBox->addItem(COM3);//列下拉框字符串数，暂且先固定3
    ui->comboBox_2->addItem(COM1);//下拉框，同样仅仅add一个QString字符，它会随着行号的增加，自动++


    //分配表格固定大小
    //初始行号设置为1，放置查询时检索未输入数据的表格，导致数组越界，程序崩溃
    //因此，需要增加行号，手动添加，且添加的表格行，需要输入满内容
    ui->tableWidget->setRowCount(1);
    ui->tableWidget->setColumnCount(3);

    //自动调整窗口列表的【宽、高】---适应窗口大小
    //setSectionResizeMode：设置整个表头的自适应模式
    //QHeaderView::Stretch ：1 根据空间，自动改变列宽，用户与程序不能改变列宽
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    QTableWidgetItem *ptem = new QTableWidgetItem("姓名");
    ui->tableWidget->setHorizontalHeaderItem(0, ptem);
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("成绩"));

    //新增行
    connect(ui->pushButton_4, &QPushButton::clicked, this, &myMainWindow::add_row);

    //删除行
    connect(ui->pushButton_5, &QPushButton::clicked, this, &myMainWindow::delete_row);

    //新增列
    connect(ui->pushButton_7, &QPushButton::clicked, this, &myMainWindow::add_column);

    //删除列
    connect(ui->pushButton_8, &QPushButton::clicked, this, &myMainWindow::delete_column);


    //对表格指定位置，添加、覆盖、修改
    connect(ui->pushButton, &QPushButton::clicked, this, [=](){

            QTableWidgetItem * ptxt = new QTableWidgetItem(ui->lineEdit->text());
            QString ps1 = ui->comboBox->currentText();
            QString ps2 = ui->comboBox_2->currentText();

            int a = atoi(ps1.toStdString().c_str());
            int b = atoi(ps2.toStdString().c_str());
            ui->tableWidget->setItem( b, a, ptxt);

            emit this->emit_pushButton(TRAM_Add_text,NULL);//发送【添加、覆盖、修改】信号
    });

    //查找指定位置本文
    connect(ui->pushButton_6, &QPushButton::clicked, this, &myMainWindow::find_table_text);

    //删除指定位置文本
    connect(ui->pushButton_3, &QPushButton::clicked, this, &myMainWindow::delete_table_text);



    QStatusBar * qbar = new QStatusBar(this);//状态栏
    this->setStatusBar(qbar);

    QLabel *pbel = new QLabel(this);//标签--要放到【状态栏中】


    QTimer * qtime = new QTimer(this);
    qtime->start(2000);
    //timeout：超时信号
    connect(qtime, &QTimer::timeout, this, [=](){
        //超时2000ms后，该槽函数响应超时机制
        //qDebug() <<"超时,调取系统实时时间" <<endl;

        //获取系统实时时间
        QDateTime begin_time = QDateTime::currentDateTime();
        QString time_String = begin_time.toString("yyyy-MM-dd dddd hh:mm:ss");
        //qDebug() << time_String << endl;

        pbel->setText(time_String);//给标签设置-实时时间文本
        qbar->addWidget(pbel);//把【标签】时间显示在状态栏上
    });


    connect(ui->pushButton_9, &QPushButton::clicked, this, &myMainWindow::show_hide_formnew_01_ui);


    QTime endTime = QTime::currentTime();//程序进入结束时间
    qDebug() << "一共所需运行时间："<< starttime.msecsTo(endTime) <<endl;


}

myMainWindow::~myMainWindow()
{
    delete ui;
}

void myMainWindow::emit_signals()
{
    emit this->back_page2();//【界面1】返回【界面0】的信号
}

void myMainWindow::find_table_text()
{
    //优先判断初始表格行是否为空
    if( ui->tableWidget->item(0, 0) != 0 && ui->tableWidget->item(0, 1) != 0 && ui->tableWidget->item(0, 2) != 0)
    {
        int j = 0;
        //清空查找框中的所有文本
        ui->textEdit->clear();
        //获取当前总行数
        int rowIndex = ui->tableWidget->rowCount();
        qDebug() << "rowIndex = " << rowIndex <<endl;


        //获取学号栏文本
        QString text01 = ui->lineEdit_3->text();
        //然后返回字符串地址，方便与表格中的文本地址【strcmp】
        char *t1 = (char *)text01.toStdString().c_str();
        qDebug() << text01 <<endl;

        QString text02 = ui->lineEdit_4->text();
        char *t2 = (char *)text02.toStdString().c_str();

        QString text03 = ui->lineEdit_5->text();
        char *t3 = (char *)text03.toStdString().c_str();

    //初始行号设置为1，放置查询时检索未输入数据的表格，会导致数组越界，程序崩溃
    //i <= rowIndex是因为刚开始行号为1，<不会进入循环
        for(int i = 1; i <= rowIndex; i++)
        {
            char str[1024] = "";

            //列不变，行变
            QString rowdata_01 = ui->tableWidget->item(i - 1, 0)->text();
            char * rowda_01 = (char *)rowdata_01.toStdString().c_str();

            QString rowdata_02 = ui->tableWidget->item(i - 1, 1)->text();
            char * rowda_02 = (char *)rowdata_02.toStdString().c_str();

            QString rowdata_03 = ui->tableWidget->item(i - 1, 2)->text();
            char * rowda_03 = (char *)rowdata_03.toStdString().c_str();

            //只要有一个相同，就全部输出
            if( (0 == strcmp(rowda_01, t1)) || (0 == strcmp(rowda_02, t2)) || (0 == strcmp(rowda_03, t3)))
            {
                sprintf(str, "%s %s %s", rowda_01, rowda_02, rowda_03);
                ui->textEdit->append(str);
                j++;
            }
        }
        if(0 == j)
        {
            ui->textEdit->setText("未找到该学生讯息");
        }
    }
    else
    {
        QMessageBox * pfind = new QMessageBox(QMessageBox::Warning, "提示框", "表格内容为空,请输入",\
                                              QMessageBox::Ok);

        pfind->exec();
    }


    emit this->emit_find_table_text(TRAM_Find_Table_Text, NULL);
}

void myMainWindow::delete_table_text()//删除指定位置文本
{
    //得到一下两个下拉框中的选择
    QString ps1 = ui->comboBox->currentText();
    QString ps2 = ui->comboBox_2->currentText();

    int a = atoi(ps1.toStdString().c_str());
    int b = atoi(ps2.toStdString().c_str());

    //删除指定表格位置文本
    ui->tableWidget->takeItem(a, b);
    //然后行编辑器中的文本也清掉
    ui->lineEdit->clear();

    emit this->emit_delete_table_text(TRAM_Delete_Table_Text, NULL);
}

void myMainWindow::add_row()
{
    int row = ui->tableWidget->currentRow();   
    int newRow = row + 1;//当前行号 +1
    int curRow  = ui->tableWidget->rowCount();

    if(-1 == row)
    {
        newRow = curRow;
    }

    ui->tableWidget->insertRow(newRow);//新计算的行号，插入表格 +1

    QString row_String = QString::number(curRow);//int --> QString
    ui->comboBox_2->addItem(row_String);//行号发生变化 +1,下拉框comboBox_2的选项 +1

    emit this->emit_pushButton_04(TRAM_ADD_Row, NULL);//发送添加行信号
}

void myMainWindow::delete_row()
{
    int row = ui->tableWidget->currentRow();

    if(-1 != row)
    {
        ui->tableWidget->removeRow(row);
        //ui->tableWidget->setCurrentIndex(NULL);
    }

    ui->comboBox_2->removeItem(ui->tableWidget->rowCount());//行号发生变化 -1,下拉框comboBox_2的选项 -1

    //删除完成后，自适应一下宽、高
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    emit this->emit_pushButton_05(TRAM_Delete_Row,NULL);//发送删除行信号
}

void myMainWindow::add_column()
{
    int column = ui->tableWidget->currentColumn();
    int curCOlumn = ui->tableWidget->columnCount();
    int newCOlumn = column + 1;

    if(-1 == column)
    {
        newCOlumn = curCOlumn;
    }

    ui->tableWidget->insertColumn(newCOlumn);

    QString column_String = QString::number(curCOlumn);
    ui->comboBox->addItem(column_String);//列号发生变化 +1,下拉框comboBox的选项 +1
}


void myMainWindow::delete_column()
{
    int column = ui->tableWidget->currentColumn();
    if(-1 != column)
    {
        ui->tableWidget->removeColumn(column);//移除列
    }

    ui->comboBox->removeItem(ui->tableWidget->columnCount());//列号发生变化 -1,下拉框comboBox的选项 -1

    //删除完成后，自适应一下宽、高
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

void myMainWindow::show_hide_formnew_01_ui()
{
       static char flag = 0;
       if(0 == flag)
       {
           ui->widget->hide();
           flag = 1;
       }
       else
       {
           ui->widget->show();
           flag = 0;
       }

       emit this->emit_show_hide_formnew_01_ui(TRAM_Show_Hide_Formnew_01_Ui, NULL);//发送隐藏、显示【formnew_01_ui表格】
}
